package co.yixiang.modules.system.rest;

import co.yixiang.annotation.AnonymousAccess;
import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.VersionDesc;
import co.yixiang.modules.system.service.VersionDescService;
import co.yixiang.modules.system.dto.queryCriteria.VersionDescQueryCriteria;
import co.yixiang.modules.system.dto.VersionDescDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author DB
* @date 2020-09-08
*/
@AllArgsConstructor
@Api(tags = "版本信息管理")
@RestController
@RequestMapping("/api/versionDesc")
public class VersionDescController {

    private final VersionDescService versionDescService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','versionDesc:list')")
    public void download(HttpServletResponse response, VersionDescQueryCriteria criteria) throws IOException {
        versionDescService.download(Convert.toList(VersionDescDto.class, versionDescService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询版本信息")
    @ApiOperation("查询版本信息")
    @PreAuthorize("@el.check('admin','versionDesc:list')")
    public ResponseEntity<Object> getVersionDescs(VersionDescQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(versionDescService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增版本信息")
    @ApiOperation("新增版本信息")
    @PreAuthorize("@el.check('admin','versionDesc:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody VersionDesc resources){
        return new ResponseEntity<>(versionDescService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改版本信息")
    @ApiOperation("修改版本信息")
    @PreAuthorize("@el.check('admin','versionDesc:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody VersionDesc resources){
        versionDescService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除版本信息")
    @ApiOperation("删除版本信息")
    @PreAuthorize("@el.check('admin','versionDesc:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        versionDescService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("查看版本信息")
    @ApiOperation("查看版本信息")
    @AnonymousAccess
    @PostMapping(value = "/queryVersion")
    public ResponseEntity<Object> queryVersion() {
        return  ResponseEntity.ok(versionDescService.queryVersion());
    }


}